//
//  UIViewController+YHNaivigationItem.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/9/19.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "UIViewController+YHNaivigationItem.h"
#import "UIButton+YH.h"
#import <objc/runtime.h>
#import "UIFont+YH.h"
#import "UIColor+YHStyle.h"
#import <YHConstant/YHConstant.h>
#import <YHConstant/YHDefines.h>

@implementation UIViewController (YHNaivigationItem)


- (UIButton *)yh_rightNavcButton{
    return objc_getAssociatedObject(self, @selector(yh_rightNavcButton));
}

- (void)setYh_rightNavcButton:(UIButton *)yh_rightNavcButton{
    objc_setAssociatedObject(self, @selector(yh_rightNavcButton), yh_rightNavcButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIButton *)yh_leftNavcButton{
    return objc_getAssociatedObject(self, @selector(yh_leftNavcButton));
}

- (void)setYh_leftNavcButton:(UIButton *)yh_leftNavcButton{
    objc_setAssociatedObject(self, @selector(yh_leftNavcButton), yh_leftNavcButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


-(void)yh_setNavLeftImagesOrTitles:(NSArray *)leftImages
                            handle:(void (^)(UIButton * _Nonnull, NSInteger))leftHandleBlock{
    [self yh_setNavLeftImagesOrTitles:leftImages handle:leftHandleBlock btnConfig:nil];
}

-(void)yh_setNavLeftImagesOrTitles:(NSArray *)leftImages
                            handle:(void (^)(UIButton * _Nonnull, NSInteger))leftHandleBlock
                         btnConfig:(void (^)(UIButton * _Nonnull, NSInteger))configBlock
{
    
    self.navigationItem.leftBarButtonItems = @[];
    
    if (!leftImages.count) return;
    
    NSMutableArray *rightButtons = [[NSMutableArray alloc]init];
    for (NSInteger i=0; i<leftImages.count; i++) {
        id item = leftImages[i];
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.yh_leftNavcButton = btn;
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
        btn.yh_touchAreaInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        btn.titleLabel.font = [UIFont yh_pfOfSize:15];
        [btn setTitleColor:[UIColor yh_h1] forState:UIControlStateNormal];
        btn.tag = i;
        
        if([item isKindOfClass:[UIImage class]]){
            [btn setImage:item forState:UIControlStateNormal];
            
            btn.frame = CGRectMake(0, 0, Adapted(40), Adapted(40));
            
        }else if ([item isKindOfClass:[NSString class]]){
            [btn setTitle:item forState:UIControlStateNormal];
            
            CGSize sizeFit = [btn sizeThatFits:CGSizeMake(300, Adapted(40))];
            btn.frame = CGRectMake(0, 0, sizeFit.width+10,  Adapted(40));
            
        }
        __weak typeof(&*btn)weakBtn = btn;
        
        [btn addActionHandler:^(UIButton * sender, NSInteger tag) {
            if (leftHandleBlock) {
                leftHandleBlock(weakBtn,tag);
            }
        }];
        if(configBlock){
            configBlock(btn,i);
        }
//        btn.backgroundColor = [UIColor yh_randomColor];
        UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [rightButtons addObject:rightBtnItem];
    }
    rightButtons = (NSMutableArray *)[[rightButtons reverseObjectEnumerator] allObjects];
    self.navigationItem.leftBarButtonItems = rightButtons.copy;
}

-(void)yh_setNavRightImagesOrTitles:(NSArray *)rightImages
                             handle:(void (^ _Nullable)(UIButton * btn, NSInteger index))rightHandleBlock{
    [self yh_setNavRightImagesOrTitles:rightImages handle:rightHandleBlock btnConfig:nil];
}

-(void)yh_setNavRightImagesOrTitles:(NSArray *)rightImages
                             handle:(void (^)(UIButton * _Nonnull, NSInteger))rightHandleBlock
                          btnConfig:(void (^)(UIButton * _Nonnull, NSInteger))configBlock
{
    if (!rightImages.count) return;
    
    NSMutableArray *rightButtons = [[NSMutableArray alloc]init];
    for (NSInteger i=0; i<rightImages.count; i++) {
        id item = rightImages[i];
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.yh_rightNavcButton = btn;
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
        btn.yh_touchAreaInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        btn.titleLabel.font = [UIFont yh_pfOfSize:15];
        [btn setTitleColor:[UIColor yh_h1] forState:UIControlStateNormal];
        btn.tag = i;
        
        if([item isKindOfClass:[UIImage class]]){
            [btn setImage:item forState:UIControlStateNormal];
            
            
            btn.frame = CGRectMake(0, 0, Adapted(40), Adapted(40));
            
        }else if ([item isKindOfClass:[NSString class]]){
            [btn setTitle:item forState:UIControlStateNormal];
            
            CGSize sizeFit = [btn sizeThatFits:CGSizeMake(300, Adapted(40))];
            btn.frame = CGRectMake(0, 0, sizeFit.width+10,  Adapted(40));
            
        }
        __weak typeof(&*btn)weakBtn = btn;
        
        [btn addActionHandler:^(UIButton * sender, NSInteger tag) {
            if (rightHandleBlock) {
                rightHandleBlock(weakBtn,tag);
            }
        }];
        if(configBlock){
            configBlock(btn,i);
        }
        
        if(rightButtons.count > 0){
            UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            spaceItem.width = -5;
            [rightButtons addObject:spaceItem];
        }
        
        UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [rightButtons addObject:rightBtnItem];
    }
    rightButtons = (NSMutableArray *)[[rightButtons reverseObjectEnumerator] allObjects];
    self.navigationItem.rightBarButtonItems = rightButtons.copy;
}


@end
